<?php
namespace app\index\controller;
use think\Controller;
use wxpay\database\WxPayUnifiedOrder;
use wxpay\database\WxPayResults;
use wxpay\NativePay;
use wxpay\WxPayConfig;
use wxpay\WxPayApi;
use wxpay\WxPayNotify;
use wxpay\PayNotifyCallBack;

class Wxpay extends Controller
{
    public function notify()
    {
		$weixinData = file_get_contents("php://input");
		file_put_contents('D:/wamp64/www/tp5/public/static/tem/2.txt', $weixinData, FILE_APPEND);
	//	$weixinData = "<root> <user>月光光abcd</user> <pvs>13002</pvs><ips> <baidu_ip>1200</baidu_ip><google_ip>1829</google_ip></ips> <date>2016-06-01</date></root>";    

		
		
	try{
		$resultObj = new WxPayResults();
		$weixinData = $resultObj->Init($weixinData);
	}catch(\Exception $e){
		//支付出现异常向微信返回值
		$resultObj->setData('return_code', 'FAIL');
		$resultObj->setData('return_msg', $e->getMessage());
		return $resultObj->toXml();
		}
		if($weixinData['return_code'] === 'FAIL' || $weixinData['return_code'] !== 'SUCCESS'){
			$resultObj->setData('return_code', 'FAIL');
		    $resultObj->setData('return_msg', 'error');
			return $resultObj->toXml();			
			}
	/*	$resultObj->setData('return_code', 'SUCCESS');
		$resultObj->setData('return_msg', 'OK');
		return $resultObj->toXml();  */
		
		//var_dump($weixinData);
		
		//根据out_trade_no查询订单数据
		$outTradeNo = $weixinData['out_trade_no'];
		$order = model('Order')->get(['out_trade_no'=>$outTradeNo]);
		if(!$order || $order->pay_status == 1){
			$resultObj->setData('return_code', 'SUCCESS');
		    $resultObj->setData('return_msg', 'OK');
			return $resultObj->toXml();
			
			}
		//更新数据  订单表  商品表
		try{
			$orderRes = model('Order')->updateOrderByOutTradeNo($outTradeNo, $weixinData);
			model('Deal')->updateBuyCountById($order->deal_id, $order->deal_count);
			//优惠券生成
			$coupons = [
			    'sn' => $outTradeNo,
				'password' => $rand(10000,99999),
				'user_id' => $order->user_id,
				'deal_id' => $order->deal_id,
				'order_id' => $order->id,
	
			];
			model('Coupons')->add($coupons);
			//发送邮件通知消费者    异步发送
			
			}catch(\Exception $e){
				
				return false;
				}
		$resultObj->setData('return_code', 'SUCCESS');
		$resultObj->setData('return_msg', 'OK');
		return $resultObj->toXml();
		
		
    }
	
	public function wxpayCode($id)
    {
	    $notify = new NativePay();
		$input = new WxPayUnifiedOrder();
		$input -> setBody("支付0.01元");
		$input -> setAttach("支付0.01元");
		$input -> setOutTradeNo(WxPayConfig::MCHID.date("YmdHis"));
		$input -> setTotalFee("1");
		$input -> setTimeStart(date("YmdHis"));
		$input -> setTimeExpire(date("YmdHis", time()+600));
		$input -> setGoodsTag("QRCode");
		$input -> setNotifyUrl("/index,php/index/weixinpay/notify");
		$input -> setTradeType("NATIVE");
		$input -> setProductId("$id");
		$result = $notify->getPayUrl($input);
		
		if(empty($result["code_url"])){
			  $url = '';
			}else{
				$url = $result["code_url"];
				}
	    return '<img alt="扫码支付" 
		        src="/weixin/example/qrcode.php?data='.urlencode($url).'" 
				style=" width: 300px; height: 300px" /> ';
	
	
	}	


}
